package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐管理")
@Slf4j
public class SetmealController {

    //注入service对象
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping()
    @ApiOperation(value = "新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId") //清楚缓存
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐：{}",setmealDTO);

        //调用service层新增
        setmealService.save(setmealDTO);

        //返回结果给前端
        return Result.success("新增套餐成功");

    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation(value = "套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页查询：{}",setmealPageQueryDTO);

        //调用service层查询
        PageResult pageResult = setmealService.page(setmealPageQueryDTO);

        //返回结果给前端
        return Result.success(pageResult);
    }

    /**
     * 根据id批量删除套餐
     * @return
     */
    @ApiOperation(value = "根据id批量删除套餐")
    @DeleteMapping()
    @CacheEvict(cacheNames = "setmealCache",allEntries = true) //清楚缓存
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("根据id批量删除套餐：{}",ids);

        //调用service层删除
        setmealService.deleteByIds(ids);

        //返回结果给前端
        return Result.success();
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询套餐信息")
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("根据id查询套餐信息：{}",id);

        //调用service层查询
        SetmealVO setmealVO = setmealService.getById(id);

        //返回结果给前端
        return Result.success(setmealVO);
    }

    /**
     * 根据id修改套餐信息
     * @param setmealDTO
     * @return
     */
    @ApiOperation(value = "修改套餐信息")
    @PutMapping()
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐信息：{}",setmealDTO);

        //调用service层修改
        setmealService.update(setmealDTO);

        //返回结果给前端
        return Result.success();
    }

    /**
     * 启售或停售套餐
     * @param status
     * @param id
     * @return
     */
    @ApiOperation(value = "启售或停售套餐")
    @PostMapping("status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("启售或停售套餐：{}",status,id);

        //调用service层修改
        setmealService.startOrStop(status,id);

        //返回结果给前端
        return Result.success();
    }
}
